package com.weihe.toolkit.ftp4j;

import java.io.File;

public final class PathToolkit {
	
	public static final String FILE_SEPARATOR = "/";
	
    private PathToolkit() {
    }

    public static String formatPath4File(String path) {
        String reg0 = "\\\\+";
        String reg = "\\\\+|/+";
        String temp = path.trim().replaceAll(reg0, "/");
        temp = temp.replaceAll(reg, "/");
        if (temp.length() > 1 && temp.endsWith("/")) {
            temp = temp.substring(0, temp.length() - 1);
        }
        temp = temp.replace('/', File.separatorChar);
        return temp;
    }

    public static String formatPath4FTP(String path) {
        String reg0 = "\\\\+";
        String reg = "\\\\+|/+";
        String temp = path.trim().replaceAll(reg0, "/");
        temp = temp.replaceAll(reg, "/");
        if (temp.length() > 1 && temp.endsWith("/")) {
            temp = temp.substring(0, temp.length() - 1);
        }
        return temp;
    }

    public static String genParentPath4FTP(String path) {
        String pp = new File(path).getParent();
        if (pp == null)
            return null;
        else
            return formatPath4FTP(pp);
    }
}